/**
 * depend
 * jQuery
 * jQuery from
 * LocalData.js
 * LocalConfig.js
 */
class InitSetting
{
    constructor(key, schema)
    {
        this.schema = this.isset(schema) ? schema : {};
        let self = this;
        // get localStrong config
        if(!localStorage.getItem(key)) {
            this.popup().initForm(this.schema);
        }

        jQuery('body').append('<span id="setting-button" style="border: 1px solid gray;z-index: 999;position: fixed;bottom: 5px;right: 5px;cursor: pointer;color: red;font-weight: bold;">Setting</span>');
        jQuery('#setting-button').on('click', function() {
            self.popup().initForm();
        });
    }

    // show popup
    popup(markId)
    {
		let self = this;
		let layout = `
        <section id="create-issue-dialog" class="aui-dialog2 aui-layer jira-dialog2 jira-dialog-core aui-dialog2-large jira-dialog-open jira-dialog-content-ready" role="dialog" aria-labelledby="jira-dialog2__heading" style="z-index: 3000;" data-aui-focus="false" data-aui-blanketed="true" open="" tabindex="-1">
        <header class="aui-dialog2-header jira-dialog-core-heading">
        <h2 id="jira-dialog2__heading" title="Sync Helper Setting Dashboard">Setting Dashboard</h2>
        </header>
        <div class="aui-dialog2-content jira-dialog-core-content">
        <div class="qf-container">
        <div class="form-footer">
        <div class="dialog-error">
        <div id="setting-dashboard"><form class="aui"></form></div>
        </div>
        </div>
        </div>
        </div>
        <footer class="aui-dialog2-footer">
        <div class="buttons-container form-footer">
        <div class="buttons">
        <span class="throbber"></span>
        <button class="aui-button aui-button-link cancel" id="aui-dialog-close" resolved="">Close</button>
        </div></div></footer></section>
        <div id="jira-bg-mark" aria-hidden="true" class="aui-blanket" tabindex="0" style="z-index: 2980;"></div>
`;

        this.markId = markId;
        if (!this.isset(markId)) {
            this.markId = 'setting-dashboard';
        }

        jQuery('body').append(layout);

        jQuery('.aui-blanket').on('click', function() {
            jQuery('#jira-bg-mark').remove();
            jQuery('#create-issue-dialog').remove();
        });
        jQuery('#aui-dialog-close').on('click', function() {
            jQuery('#jira-bg-mark').remove();
            jQuery('#create-issue-dialog').remove();
        });
        return this;
    }

    initForm(schema)
    {
        if (this.isset(schema)) {
            this.schema = schema;
        }

        jQuery('#' + this.markId + ' form').jsonForm(this.schema);
        return this;
    }

    isset(value)
    {
        return '[object Undefined]' != Object.prototype.toString.call(value);
    }
}


/*
var schema = {
    "schema": {
        "source": {
            "type": "object",
            "properties": {
                "host": {
                    "type": "string",
                    "title": "Source Host",
                    "required": true
                },
                "token": {
                    "type": "string",
                    "title": "Source Token",
                    "required": true
                },
                "searchMax": {
                    "type": "string",
                    "title": "Source Search Max",
                    "required": true
                }
            }
        },
        "target": {
            "type": "object",
            "properties": {
                "host": {
                    "type": "string",
                    "title": "Target Host",
                    "required": true
                },
                "token": {
                    "type": "string",
                    "title": "Target Token",
                    "required": true
                },
                "searchMax": {
                    "type": "string",
                    "title": "Target Search Max",
                    "required": true
                }
            }
        },
        "flybook": {
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "title": "Notice Host",
                    "required": true
                }
            }
        },
        "resources": {
            "type":"textarea",
            "title":"Resources",
        },
        "lavelMapping": {
            "type":"textarea",
            "title":"Lavel Mapping",
        }
    },
    "form": [{
            "type": "fieldset",
            "title": "Source",
            "items": [
                "source.host",
                "source.token",
                "source.searchMax"
            ]
        },
        {
            "type": "fieldset",
            "title": "Target",
            "items": [
                "target.host",
                "target.token",
                "target.searchMax"
            ]
        },
        {
            "type": "fieldset",
            "title": "Notice Url",
            "items": [
                "flybook.url"
            ]
        },
        {
            'key':'resources'
        },
        {
            'key':'lavelMapping'
        },
        {
            "type": "submit",
            "title": "Save",
            "htmlClass": "aui-button aui-button-primary aui-style"
        }
    ],
    "onSubmit": function(errors, values) {
        if (!errors) {
            // set to localStrong
            let config = JSON.parse(localStorage.getItem('config'));
			config = config ? config : {};
            Object.keys(values).forEach(function(key) {
                config[key] = values[key];
            });
			localStorage.setItem('config', JSON.stringify(config));
            console.log('Update setting success.');
            alert('Update setting success.');
        } else {
            console.error('Error', errors);
        }

        return false;
    },
    "params": {
        "fieldHtmlClass": "textfield text long-field"
    }
};
var config = JSON.parse(localStorage.getItem('config'));
try {
    if (config) {
        schema.value = {
            "source":{
                "host":config.source.host,
                "token":config.source.token,
                "searchMax": config.source.searchMax
            },
            "target":{
                "host":config.target.host,
                "token":config.target.token,
                "searchMax": config.target.searchMax
            },
            "resources":config.resources,
            "lavelMapping":config.lavelMapping
        };
    }
} catch(e) {
    console.error('##Can not load exist config!', e);
}
var formSetting = new InitSetting('config', schema);
//*/